using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class CreditCardInformationsController : Controller
    {
        public CreditCardInformationsController(IRepository<CreditCardInformation> creditCardInformationRepository) {
            Check.Require(creditCardInformationRepository != null, "creditCardInformationRepository may not be null");

            this.creditCardInformationRepository = creditCardInformationRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<CreditCardInformation> creditCardInformations = creditCardInformationRepository.GetAll();
            return View(creditCardInformations);
        }

        [Transaction]
        public ActionResult Show(int id) {
            CreditCardInformation creditCardInformation = creditCardInformationRepository.Get(id);
            return View(creditCardInformation);
        }

        public ActionResult Create() {
            CreditCardInformationFormViewModel viewModel = CreditCardInformationFormViewModel.CreateCreditCardInformationFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(CreditCardInformation creditCardInformation) {
            if (ViewData.ModelState.IsValid && creditCardInformation.IsValid()) {
                creditCardInformationRepository.SaveOrUpdate(creditCardInformation);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The creditCardInformation was successfully created.";
                return RedirectToAction("Index");
            }

            CreditCardInformationFormViewModel viewModel = CreditCardInformationFormViewModel.CreateCreditCardInformationFormViewModel();
            viewModel.CreditCardInformation = creditCardInformation;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            CreditCardInformationFormViewModel viewModel = CreditCardInformationFormViewModel.CreateCreditCardInformationFormViewModel();
            viewModel.CreditCardInformation = creditCardInformationRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(CreditCardInformation creditCardInformation) {
            CreditCardInformation creditCardInformationToUpdate = creditCardInformationRepository.Get(creditCardInformation.Id);
            TransferFormValuesTo(creditCardInformationToUpdate, creditCardInformation);

            if (ViewData.ModelState.IsValid && creditCardInformation.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The creditCardInformation was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                creditCardInformationRepository.DbContext.RollbackTransaction();

				CreditCardInformationFormViewModel viewModel = CreditCardInformationFormViewModel.CreateCreditCardInformationFormViewModel();
				viewModel.CreditCardInformation = creditCardInformation;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(CreditCardInformation creditCardInformationToUpdate, CreditCardInformation creditCardInformationFromForm) {
			creditCardInformationToUpdate.Customer = creditCardInformationFromForm.Customer;
			creditCardInformationToUpdate.CreditCardNumber = creditCardInformationFromForm.CreditCardNumber;
			creditCardInformationToUpdate.ExpirationYear = creditCardInformationFromForm.ExpirationYear;
			creditCardInformationToUpdate.ExpirationMonth = creditCardInformationFromForm.ExpirationMonth;
			creditCardInformationToUpdate.CVV = creditCardInformationFromForm.CVV;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The creditCardInformation was successfully deleted.";
            CreditCardInformation creditCardInformationToDelete = creditCardInformationRepository.Get(id);

            if (creditCardInformationToDelete != null) {
                creditCardInformationRepository.Delete(creditCardInformationToDelete);

                try {
                    creditCardInformationRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the creditCardInformation from being deleted. " +
						"Another item likely depends on this creditCardInformation.";
                    creditCardInformationRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The creditCardInformation could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the CreditCardInformation form for creates and edits
		/// </summary>
        public class CreditCardInformationFormViewModel
        {
            private CreditCardInformationFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static CreditCardInformationFormViewModel CreateCreditCardInformationFormViewModel() {
                CreditCardInformationFormViewModel viewModel = new CreditCardInformationFormViewModel();
                
                return viewModel;
            }

            public CreditCardInformation CreditCardInformation { get; internal set; }
        }

        private readonly IRepository<CreditCardInformation> creditCardInformationRepository;
    }
}
